package com.weng.sso.core.crypt;

import java.security.Key;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.Key;
import java.security.spec.AlgorithmParameterSpec;

/**
 * @author wengzhonghui
 * @date 2019/4/13 14:52
 */
public class DES {
    private static final byte[] IV_PARAMS_BYTES = "hyh_sso".getBytes();

    public DES() {
    }

    public static String encrypt(String key, String data) {
        if (data == null) {
            return null;
        } else {
            try {
                DESKeySpec dks = new DESKeySpec(key.getBytes());
                SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
                Key secretKey = keyFactory.generateSecret(dks);
                Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
                AlgorithmParameterSpec paramSpec = new IvParameterSpec(IV_PARAMS_BYTES);
                cipher.init(1, secretKey, paramSpec);
                byte[] bytes = cipher.doFinal(data.getBytes());
                return byte2hex(bytes);
            } catch (Exception var8) {
                var8.printStackTrace();
                return data;
            }
        }
    }

    public static String decrypt(String key, String data) {
        if (data == null) {
            return null;
        } else {
            try {
                DESKeySpec dks = new DESKeySpec(key.getBytes());
                SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
                Key secretKey = keyFactory.generateSecret(dks);
                Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
                AlgorithmParameterSpec paramSpec = new IvParameterSpec(IV_PARAMS_BYTES);
                cipher.init(2, secretKey, paramSpec);
                return new String(cipher.doFinal(hex2byte(data.getBytes())));
            } catch (Exception var7) {
                var7.printStackTrace();
                return data;
            }
        }
    }

    private static String byte2hex(byte[] b) {
        StringBuilder hs = new StringBuilder();

        for(int n = 0; b != null && n < b.length; ++n) {
            String stmp = Integer.toHexString(b[n] & 255);
            if (stmp.length() == 1) {
                hs.append('0');
            }

            hs.append(stmp);
        }

        return hs.toString().toUpperCase();
    }

    private static byte[] hex2byte(byte[] b) {
        if (b.length % 2 != 0) {
            throw new IllegalArgumentException();
        } else {
            byte[] b2 = new byte[b.length / 2];

            for(int n = 0; n < b.length; n += 2) {
                String item = new String(b, n, 2);
                b2[n / 2] = (byte)Integer.parseInt(item, 16);
            }

            return b2;
        }
    }
}
